Download libraries:
library(sf)
## Warning: package 'sf' was built under R version 4.2.3
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(tidyverse)
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor)
##
## Attaching package: 'janitor'
##
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
library(stringr)
library(mapview)
## Warning: package 'mapview' was built under R version 4.2.3
Step 2.
Read the NYC postal areas in Shapefiles into sf objects. As NYC DOH
publishes COVID-19 data by zip code, we will utilize the postal area
data later.
# Set working directory
wd <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(wd)
# add NYC zip code shapefile
zipcode <- st_read("Data/HW_Data/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source
## `C:\Users\amyca\OneDrive\Documents\GTECH7852_R\R-spatial\GTECH7852_HW\Data\HW_Data\ZIP_CODE_040114.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 263 features and 12 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
Step 3
Read and process the NYS health facilities spreadsheet data. Create
sf objects from geographic coordinates.
# Add NYC health facilties data (points)
HealthFacilties_data <- read.csv("Data/HW_Data/NYS_Health_Facility.csv")
# Clean data
HealthFacilties_data <- clean_names(HealthFacilties_data)
# Turning health facilties csv into sf.
# Process the location column using stringr
leftPos <- stringr::str_locate(HealthFacilties_data$facility_location, "\\(")[,1]
rghtPos <- stringr::str_locate(HealthFacilties_data$facility_location, "\\)")[,1]
# Get the coordinates text
HealthFacilties_data$facility_location %>% stringr::str_sub(leftPos+1, rghtPos -1) -> HealthFacilties_data$coords
cmmaPos <- stringr::str_locate(HealthFacilties_data$coords, ", ")
#Get the numeric coordinates
HealthFacilties_data$Y <- stringr::str_sub(HealthFacilties_data$coords, 1, cmmaPos[,1]-1) %>% as.numeric()
HealthFacilties_data$X <- stringr::str_sub(HealthFacilties_data$coords, cmmaPos[,2]+1) %>% as.numeric()
# Take out the rows without coordinates and make a sf object
st_as_sf(HealthFacilties_data %>% tidyr::drop_na(X, Y), coords = c('X', 'Y')) -> HealthFacilties_SF
# Assign coordinate system
st_crs(HealthFacilties_SF) <- 4326
view(HealthFacilties_SF)
#Create sf objects from geographic coordinates for NYC
NYC_HealthFacilties_SF <- HealthFacilties_SF %>%
filter(facility_county %in% c("Bronx", "Kings","Queens", "New York","Richmond"))
view(NYC_HealthFacilties_SF)
Step 4.
Read and process the NYS retail food stores data. Create sf objects
from geographic coordinates for NYC.
# Add NYC food retails store data (points)
food_retails_xy <- read.csv("Data/HW_Data/nys_retail_food_store_xy.csv", fileEncoding = "Latin1", check.names = F)
# Clean data
food_retails_xy <- clean_names(food_retails_xy)
food_retails_NY <- food_retails_xy %>%
filter(zip_code > 7000) %>%
filter(i_county %in% c("Bronx", "Kings","Queens", "New York","Richmond")) %>%
filter(!is.na(x)) %>%
filter(!is.na(y))
# Turning csv into sf. Process the location column using stringr
st_as_sf(food_retails_NY %>% tidyr::drop_na(x, y), coords = c('x', 'y')) -> food_retailsNY_SF
# Assign coordinate system
st_crs(food_retailsNY_SF) <- 4326
Step 5.
Use simple mapping method, either based on ggmap+ggplot or mapview,
with a basemap to verify the above datasets in terms of their geometry
locations
Zip codes for NYC map
mapview(zipcode, zcol = "COUNTY")
Food Retail Map
mapview(food_retailsNY_SF)